//
//  RBCateListVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/9/6.
//

#import "RBCateListVC.h"
#import "RBGoodsDetailVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "RanTool.h"
#import "UIViewController+RBAdd.h"
#import "RBNavigationBar.h"
#import "RBTextField.h"
#import <UIControl+YYAdd.h>
#import "RBCateListCell.h"
#import "RBCateListGridCell.h"
#import "RBZoneChooseVC.h"

@interface RBCateListVC ()<UITextFieldDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong)RBNavigationBar *navigationBar;
@property(nonatomic, strong)RBTextField *searchF;
@property(nonatomic, strong)UIView *filterView;
@property(nonatomic, strong)UIView *appraiseFilterBtn;
@property(nonatomic, strong)UIView *salesFilterBtn;
@property(nonatomic, strong)UIView *zoneFilterBtn;
@property(nonatomic, strong)UIView *filterFilterBtn;
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, assign)NSInteger style;

@end

@implementation RBCateListVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self fillData];
}

#pragma mark - Private

- (void)setup
{
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
    self.navigationBar.hidden = NO;
    self.filterView.hidden = NO;
}

- (void)fillData
{
    __weak typeof(self) weakSelf = self;
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            for (NSInteger i = 0; i < 20; i++)
            {
                [arrayM addObject:({
                    RBCellModel *cellModel = RBCellModel.new;
                    cellModel.cellSelectBlock = ^(NSIndexPath *indexPath) {
                        RBGoodsDetailVC *vc = RBGoodsDetailVC.new;
                        [weakSelf.navigationController pushViewController:vc animated:YES];
                    };
                    cellModel;
                })];
            }
            arrayM;
        });
        group;
    })];
    self.style = self.style;
}

#pragma mark - Network

#pragma mark - Event

- (void)switchBtnTapped
{
    if (self.style == 0)
    {
        self.style = 1;
    }
    else
    {
        self.style = 0;
    }
}

- (void)appraiseFilterBtnTapped
{
    NSInteger tag = self.appraiseFilterBtn.tag + 1;
    if(tag > 2)
    {
        tag = 1;
    }
    [self setFilterBtn:self.appraiseFilterBtn withTag:tag];
    [self setFilterBtn:self.salesFilterBtn withTag:0];
}

- (void)salesFilterBtnTapped
{
    NSInteger tag = self.salesFilterBtn.tag + 1;
    if(tag > 2)
    {
        tag = 1;
    }
    [self setFilterBtn:self.salesFilterBtn withTag:tag];
    [self setFilterBtn:self.appraiseFilterBtn withTag:0];
}

- (void)zoneFilterBtnTapped
{
    RBZoneChooseVC *vc = RBZoneChooseVC.new;
    [self presentVc:vc];
}

- (void)filterFilterBtnTapped
{
    
}

#pragma mark - Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

kRBCollectionViewDataSourceAndDelegate(self.dataSource);

#pragma mark - Setter

- (void)setFilterBtn: (UIView *)filterBtn withTag: (NSInteger)tag
{
    filterBtn.tag = tag;
    UIImageView *upImgView = (UIImageView *)[filterBtn viewWithTag:333];
    UIImageView *dowImgView = (UIImageView *)[filterBtn viewWithTag:444];
    if (tag == 0)
    {
        upImgView.image = [UIImage imageNamed:@"shopFilterUp"];
        dowImgView.image = [UIImage imageNamed:@"shopFilterDown"];
    }
    else if (tag == 1)
    {
        upImgView.image = [UIImage imageNamed:@"shopFilterUpSel"];
        dowImgView.image = [UIImage imageNamed:@"shopFilterDown"];
    }
    else if (tag == 2)
    {
        upImgView.image = [UIImage imageNamed:@"shopFilterUp"];
        dowImgView.image = [UIImage imageNamed:@"shopFilterDownSel"];
    }
}

- (void)setStyle:(NSInteger)style
{
    _style = style;
    for (RBGroupModel *group in self.dataSource)
    {
        for (RBCellModel *cellModel in group.cellModels)
        {
            if (style == 0)
            {
                group.sectionInset = UIEdgeInsetsMake(Zoom(10), 0, Zoom(10), 0);
                group.minimumInteritemSpacing = Zoom(10);
                group.minimumLineSpacing = Zoom(10);
                cellModel.reuseCellIdentifier = NSStringFromClass(RBCateListGridCell.class);
                cellModel.itemSize = CGSizeMake(Zoom(350), Zoom(131));
            }
            else
            {
                group.sectionInset = UIEdgeInsetsMake(Zoom(10), Zoom(15), Zoom(10), Zoom(15));
//                group.minimumInteritemSpacing = Zoom(10);
                group.minimumLineSpacing = Zoom(10);
                cellModel.reuseCellIdentifier = NSStringFromClass(RBCateListCell.class);
                cellModel.itemSize = CGSizeMake(Zoom(167), Zoom(242));
            }
        }
    }
    [self.collectionView reloadData];
}

#pragma mark - Getter

- (RBNavigationBar *)navigationBar
{
    if (!_navigationBar)
    {
        self.navigationBar = ({
            RBNavigationBar *bar = RBNavigationBar.new;
            bar.bgColor = UIColor.whiteColor;
            bar.backgroundAlpha = 1;
            bar.needGradientView = NO;
            [self.view addSubview:bar];
            [bar mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.height.mas_equalTo(kRBStatusBarHeight + kRBNavigationBarHeight);
            }];
            [bar.naviBar addBackBtnWithWidth:50 image:[UIImage imageNamed:@"backBlack"]];
            
            UIImageView *switchBtn = [RanTool imgViewWithMode:0 radius:0 imgName:@"b1.jpg"];
            [switchBtn extendResponseArea];
            [switchBtn rbAddTapGesWithTarget:self action:@selector(switchBtnTapped)];
            [bar.naviBar addSubview:switchBtn];
            [switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(-Zoom(12));
                make.height.width.mas_equalTo(22);
                make.centerY.mas_equalTo(0);
            }];
            
            UIView *searchView = UIView.new;
            [bar.naviBar addSubview:searchView];
            [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(bar.naviBar.getBackBtn.mas_right);
                make.right.mas_equalTo(switchBtn.mas_left).offset(-Zoom(17));
                make.height.mas_equalTo(36);
                make.centerY.mas_equalTo(0);
            }];
            searchView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
            [searchView rbSetCornerRadius:18];
            
            
            UIImageView *imgView = UIImageView.new;
            imgView.image = [UIImage imageNamed:@"homeSearchIcon"];
            [searchView addSubview:imgView];
            [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(14);
                make.centerY.mas_equalTo(0);
                make.width.height.mas_equalTo(16);
            }];
            
            RBTextField *textF = RBTextField.new;
            self.searchF = textF;
            textF.delegate = self;
            [searchView addSubview:textF];
            [textF mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.mas_equalTo(0);
                make.left.mas_equalTo(imgView.mas_right).offset(8);
                make.right.mas_equalTo(-20);
            }];
            textF.placeholder = @"请输入商品名称";
            textF.font = [UIFont systemFontOfSize:12];
            textF.textColor = [RBThemeConfig textBlackColor];
            textF.keyboardType = UIKeyboardTypeWebSearch;
            
            bar;
        });
    }
    return _navigationBar;
}

- (UIView *)filterView
{
    if (!_filterView)
    {
        self.filterView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.top.mas_equalTo(self.navigationBar.mas_bottom);
                make.height.mas_equalTo(Zoom(37));
            }];
            
            UIStackView *stackView = UIStackView.new;
            [view addSubview:stackView];
            [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.mas_equalTo(0);
                make.left.mas_equalTo(Zoom(50));
                make.right.mas_equalTo(-Zoom(50));
            }];
            stackView.distribution = UIStackViewDistributionEqualSpacing;
            
            self.appraiseFilterBtn = [self filterBtnWithTitle:@"评价"];
            [self.appraiseFilterBtn rbAddTapGesWithTarget:self action:@selector(appraiseFilterBtnTapped)];
            [stackView addArrangedSubview:self.appraiseFilterBtn];
            
            self.salesFilterBtn = [self filterBtnWithTitle:@"销量"];
            [self.salesFilterBtn rbAddTapGesWithTarget:self action:@selector(salesFilterBtnTapped)];
            [stackView addArrangedSubview:self.salesFilterBtn];
            
            self.zoneFilterBtn = [self filterBtnWithTitle:@"地区"];
            [self.zoneFilterBtn rbAddTapGesWithTarget:self action:@selector(zoneFilterBtnTapped)];
            [stackView addArrangedSubview:self.zoneFilterBtn];
            
            self.filterFilterBtn = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"筛选"];
            [self.filterFilterBtn rbAddTapGesWithTarget:self action:@selector(filterFilterBtnTapped)];
            [stackView addArrangedSubview:self.filterFilterBtn];
            
            view;
        });
    }
    return _filterView;
}

- (UIView *)filterBtnWithTitle: (NSString *)title
{
    UIView *view = UIView.new;
    [view h_view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:title] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
    [view h_view_addView:({
        UIView *view = UIView.new;
        [view view_addView:({
            UIImageView *view = [RanTool imgViewWithMode:0 radius:0 imgName:@"shopFilterUp"];
            view.tag = 333;
            view;
        }) withHeight:Zoom(5) width:Zoom(7) top:0 bottom:0 left:0 right:0 centerX:0];
        [view view_addView:({
            UIImageView *view = [RanTool imgViewWithMode:0 radius:0 imgName:@"shopFilterDown"];
            view.tag = 444;
            view;
        }) withHeight:Zoom(5) width:Zoom(7) top:Zoom(2) bottom:0 left:0 right:0 centerX:0];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(6) right:0 centerY:YES];
    return view;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
//            layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
            UICollectionView *view = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = [RBThemeConfig bgGrayColor];
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.top.mas_equalTo(self.filterView.mas_bottom);
            }];
            view;
        });
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}


@end
